package thunderbirdgtd.tests;

import static org.junit.Assert.assertTrue;

import java.awt.Color;
import java.io.File;
import java.util.LinkedList;

import org.junit.Test;

import thunderbirdgtd.GTDParent;
import thunderbirdgtd.entities.Project;
import thunderbirdgtd.entities.Tag;
import thunderbirdgtd.global.XmlUtil;


public class XMLTest {

	//Testa se os projectos s�o escritos correctamente para XML
	@Test
	public void testSaveProjectsToXML(){
		XmlUtil xml = new XmlUtil();
		LinkedList<Project> projects = new LinkedList<Project>();
		Project project = new Project("project");
		Tag a = new Tag("taga");
		Tag b = new Tag("tagb");
		a.setColor(new Color(255, 255, 255));
		a.setTagDescription("Descri��o e tal da tag a ");
		b.setColor(new Color(25, 25, 25));
		b.setTagDescription("Descri��o e tal da tag b ");
		projects.add(project);
		
		Project project1 = new Project("project1");
		projects.add(project1);
		project.assignSubProject(project1);
		
		Project project2 = new Project("project2");
		project.assignSubProject(project2);
		projects.add(project2);
		try {
			project.addTag(a);
			project.addTag(b);
			project1.addTag(b);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String filename = "xml/test.xml";
		File file = new File(filename);
		if(file.exists())
			file.delete();
		assertTrue(!file.exists());
		assertTrue(xml.saveProjectsToXml(projects, filename));		
		assertTrue(file.exists());
	}
	
	//TODO: actions
	@Test
	public void testSaveAll(){
		// Create projects and tags
		LinkedList<Project> projects = new LinkedList<Project>();
		Project project = new Project("project");
		Tag a = new Tag("taga");
		Tag b = new Tag("tagb");
		a.setColor(new Color(255, 255, 255));
		a.setTagDescription("Descri��o e tal da tag a ");
		b.setColor(new Color(25, 25, 25));
		b.setTagDescription("Descri��o e tal da tag b ");
		projects.add(project);
		
		Project project1 = new Project("project1");
		projects.add(project1);
		project.assignSubProject(project1);
		
		Project project2 = new Project("project2");
		project.assignSubProject(project2);
		projects.add(project2);
		try {
			project.addTag(a);
			project.addTag(b);
			project1.addTag(b);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// Create parent and add projects
		GTDParent parent = new GTDParent();
		parent.projects = projects;
		
		// Save to file
		XmlUtil xml = new XmlUtil();
		String filename = "xml/test.xml";
		File file = new File(filename);
		if(file.exists())
			file.delete();
		
		// Assertions
		assertTrue(!file.exists());
		assertTrue(xml.saveProjectsToXml(projects, filename));		
		assertTrue(file.exists());
	}
}
